<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Modal\memberModal;
use App\Admin\Repositories\TryMessage;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Widgets\Alert;
use GuzzleHttp\Client;

class TryMessageController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new TryMessage(), function (Grid $grid) {
            $grid->model()->orderByDesc('status');
            $grid->column('id')->sortable();
            $grid->column('open_id');
            $grid->column('sex');
            $grid->column('open_id');
            $grid->column('uuid');
            $grid->column('age');
            $grid->column('city');
            $grid->column('occ');
            $grid->column('money');
            $grid->column('live');
            $grid->column('label');
            $grid->column('recommend','自荐');
            $grid->column('eat_type','类型');
            $grid->column('created_at','添加时间');
            $grid->column('status')->switch();
            $grid->tools(function (Grid\Tools $tools) {
                // excle 导入
                $tools->append(new memberModal());
            });
            $grid->export();

        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new TryMessage(), function (Show $show) {
            $show->field('id');
            $show->field('sex');
            $show->field('open_id');
            $show->field('age');
            $show->field('city');
            $show->field('occ');
            $show->field('money');
            $show->field('live');
            $show->field('label');
            $show->field('status');
            $show->field('uuid');
            $show->field('img');
            $show->field('evaluate');
            $show->field('name');
            $show->field('address');
            $show->field('phone');
            $show->field('remarks');
            $show->field('examine');
            $show->field('deliver');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new TryMessage(), function (Form $form) {
            $form->display('id');
            $form->text('sex');
            $form->text('open_id');
            $form->text('age');
            $form->text('city');
            $form->text('occ');
            $form->text('money');
            $form->text('live');
            $form->text('label');
            $form->text('status');
            $form->text('uuid');
            $form->text('img');
            $form->text('evaluate');
            $form->text('name');
            $form->text('address');
            $form->text('phone');
            $form->text('remarks');
            $form->text('examine');
            $form->text('deliver');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }


    /**
     * 发送模板消息
     */
    public function SendMessage(Content $content)
    {
        $template =  \App\Models\WxMessage::query()->first();
        $token = env('WX_TOKEN');
        $client = new Client(['','timeout'  => 5.0,]);
        //拿到微信access_token
        $response1 = $client->get('https://amxshop.yili.com/api/user/getAccessToken',['headers'=>['accessToken'=>$token]]);
        $data = json_decode($response1->getBody()->getContents());
        $wx_token = $data->data;
        \App\Jobs\WxMessage::dispatch($template,$wx_token,$client);
        $content->row(Alert::make('发送成功', 'Success')->success());
        $header = '模板消息';
        $content->breadcrumb('Components');
        $content->breadcrumb($header);
        return $content->header($header);
//        return '模板消息已发送';
//        return redirect('/admin/wx_message');
    }


}
